package gestion.camping.vue;

import gestion.camping.controleur.SaisonController;
import gestion.camping.dto.Saison;
import gestion.camping.utils.DateFormatUtils;
import gestion.camping.utils.MessagesUtil;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class ViewSaisonDetail extends JDialog implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = -17760458983364266L;
	
	
	private JButton btnEnregister;
	private JButton btnAnnuler;
	
	private Saison saison;
	private SaisonController controller;
	private JButton btnRetourListe;
	private JLabel lblLibelle;
	private JTextField textLibelle;
	private JLabel lblDateDebut;
	private JTextField textDateDebut;
	private JLabel lblDateFin;
	private JTextField textDateFin;


	/**
	 * Create the frame.
	 */
	public ViewSaisonDetail(SaisonController controller) {
	
		this.controller = controller; 
		this.saison = controller.getSaison();
		
		initCompoment();
		
		setModal(true);
	}
	
	public void initCompoment(){
	
		
		setBounds(0, 25, 700, 500);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0,0, 0, 0};
		gridBagLayout.rowHeights = new int[]{40, 40, 25, 25, 25, 25, 25, 25, 25, 25, 25, 0};
		gridBagLayout.columnWeights = new double[]{0.3, 0.2, 1.0, 0.3};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
		getContentPane().setLayout(gridBagLayout);
		
		btnRetourListe = new JButton(MessagesUtil.formatMessage("saison.ajout.btn.retour"));
		btnRetourListe.addActionListener(this);
		GridBagConstraints gbc_btnRetourListe = new GridBagConstraints();
		gbc_btnRetourListe.anchor = GridBagConstraints.WEST;
		gbc_btnRetourListe.insets = new Insets(0, 0, 5, 0);
		gbc_btnRetourListe.gridx = 3;
		gbc_btnRetourListe.gridy = 0;
		getContentPane().add(btnRetourListe, gbc_btnRetourListe);
		
		lblLibelle = new JLabel(MessagesUtil.formatMessage("saison.ajout.libelle"));
		GridBagConstraints gbc_lblLibelle = new GridBagConstraints();
		gbc_lblLibelle.anchor = GridBagConstraints.EAST;
		gbc_lblLibelle.insets = new Insets(0, 0, 5, 5);
		gbc_lblLibelle.gridx = 1;
		gbc_lblLibelle.gridy = 2;
		getContentPane().add(lblLibelle, gbc_lblLibelle);
		
		textLibelle = new JTextField(saison.getLibelle());
		GridBagConstraints gbc_textLibelle = new GridBagConstraints();
		gbc_textLibelle.insets = new Insets(0, 0, 5, 5);
		gbc_textLibelle.fill = GridBagConstraints.HORIZONTAL;
		gbc_textLibelle.gridx = 2;
		gbc_textLibelle.gridy = 2;
		getContentPane().add(textLibelle, gbc_textLibelle);
		textLibelle.setColumns(10);
		
		lblDateDebut = new JLabel(MessagesUtil.formatMessage("saison.ajout.date.debut"));
		GridBagConstraints gbc_lblDateDebut = new GridBagConstraints();
		gbc_lblDateDebut.anchor = GridBagConstraints.EAST;
		gbc_lblDateDebut.insets = new Insets(0, 0, 5, 5);
		gbc_lblDateDebut.gridx = 1;
		gbc_lblDateDebut.gridy = 3;
		getContentPane().add(lblDateDebut, gbc_lblDateDebut);
		
		textDateDebut = new JTextField(saison.getDateDebutString());
		GridBagConstraints gbc_textDateDebut = new GridBagConstraints();
		gbc_textDateDebut.insets = new Insets(0, 0, 5, 5);
		gbc_textDateDebut.fill = GridBagConstraints.HORIZONTAL;
		gbc_textDateDebut.gridx = 2;
		gbc_textDateDebut.gridy = 3;
		getContentPane().add(textDateDebut, gbc_textDateDebut);
		textDateDebut.setColumns(10);
		
		lblDateFin = new JLabel(MessagesUtil.formatMessage("saison.ajout.date.fin"));
		GridBagConstraints gbc_lblDateFin = new GridBagConstraints();
		gbc_lblDateFin.anchor = GridBagConstraints.EAST;
		gbc_lblDateFin.insets = new Insets(0, 0, 5, 5);
		gbc_lblDateFin.gridx = 1;
		gbc_lblDateFin.gridy = 4;
		getContentPane().add(lblDateFin, gbc_lblDateFin);
		
		textDateFin = new JTextField(saison.getDateFinString());
		GridBagConstraints gbc_textDateFin = new GridBagConstraints();
		gbc_textDateFin.insets = new Insets(0, 0, 5, 5);
		gbc_textDateFin.fill = GridBagConstraints.HORIZONTAL;
		gbc_textDateFin.gridx = 2;
		gbc_textDateFin.gridy = 4;
		getContentPane().add(textDateFin, gbc_textDateFin);
		textDateFin.setColumns(10);
		
		btnEnregister = new JButton(MessagesUtil.formatMessage("saison.ajout.btn.enregistrer"));
		btnEnregister.addActionListener(this);
		GridBagConstraints gbc_btnEnregister = new GridBagConstraints();
		gbc_btnEnregister.anchor = GridBagConstraints.EAST;
		gbc_btnEnregister.insets = new Insets(0, 0, 5, 5);
		gbc_btnEnregister.gridx = 2;
		gbc_btnEnregister.gridy = 10;
		getContentPane().add(btnEnregister, gbc_btnEnregister);
		
		btnAnnuler = new JButton(MessagesUtil.formatMessage("saison.ajout.btn.annuler"));
		btnAnnuler.addActionListener(this);
		GridBagConstraints gbc_btnAnnuler = new GridBagConstraints();
		gbc_btnAnnuler.anchor = GridBagConstraints.WEST;
		gbc_btnAnnuler.insets = new Insets(0, 0, 5, 0);
		gbc_btnAnnuler.gridx = 3;
		gbc_btnAnnuler.gridy = 10;
		getContentPane().add(btnAnnuler, gbc_btnAnnuler);
	}
	
	public void update(){
		/*mise � jour des label... */
		textLibelle.setText(saison.getLibelle());
		textDateDebut.setText(saison.getDateDebutString());
		textDateFin.setText(saison.getDateFinString());
		
		this.repaint();
	}

	public Saison getSaison() {
		return saison;
	}

	public void setSaison(Saison saison) {
		this.saison = saison;		
		update();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource() == this.btnEnregister){
			/*alimentation de l'objet saison*/
			saison.setLibelle(textLibelle.getText());
			saison.setDateDebut(DateFormatUtils.parseDate(textDateDebut.getText()));
			saison.setDateFin(DateFormatUtils.parseDate(textDateFin.getText()));
		
			controller.enregistrer(saison);
			
		}else if(e.getSource() == this.btnAnnuler){
			update();
		}else if(e.getSource() == this.btnRetourListe){
			
			this.dispose();
			controller.retour();
			
		}
	}
		
}
